package com.flyoil.petromp.ui.activity.activity_me;

import android.app.ActionBar;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cnpc.fyimageloader.utils.ImageTools;
import com.cnpc.fyimageloader.utils.ImageUtils;
import com.cnpc.fyutils.SystemUtils;
import com.cnpc.fyutils.ToastUtils;
import com.flyoil.petromp.Constant.Constants;
import com.flyoil.petromp.R;
import com.flyoil.petromp.base.BaseActivity;
import com.flyoil.petromp.view.PaintView;

/**
 * 书写签名的界面
 */
public class SetSignatureActivity extends BaseActivity {
    private TextView finish, quxiao, clear;
    private ImageView signature, img_help;
    private PaintView pain;
    private LinearLayout line_main;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;//强制设置为横屏
        //去掉标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //取消状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_set_signature;
    }

    @Override
    protected void initViews() {
        super.initViews();
        pain = $(R.id.paintView_handwriting);
        signature = $(R.id.imageview_handwriting);
        line_main = $(R.id.line_set_signature_layout);
        img_help = $(R.id.img_set_signature_help);
        line_main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        Drawable draw1 = this.getResources().getDrawable(R.mipmap.title_qingzaiciqianming);
        signature.setImageBitmap(ImageUtils.rotaingImageView(90, ImageTools.drawableToBitmap(draw1)));
        finish = $(R.id.textView_handwriting_queding);
        clear = $(R.id.textView_handwriting_qingchu);
        quxiao = $(R.id.textView_handwriting_quxiao);
        finish.setOnClickListener(onClickListener);
        clear.setOnClickListener(onClickListener);
        quxiao.setOnClickListener(onClickListener);
        img_help.setOnClickListener(onClickListener);
    }

    @Override
    protected void initEvents() {
        pain.setSize(SystemUtils.getScreenWidth(mContext),
                SystemUtils.getScreenHeight(mContext),
                new PaintView.OnMoveLisener() {

                    @Override
                    public void hideWords() {
                        signature.setVisibility(View.GONE);
                    }
                });
        pain.clear();
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.textView_handwriting_queding: //确定
                    if (!pain.isDrawed()) {
                        ToastUtils.show_toast("请先设置您的签名!");
                        return;
                    }
                    save_signatuere();
                    break;
                case R.id.textView_handwriting_qingchu: //清除
                    pain.clear();
                    break;
                case R.id.textView_handwriting_quxiao:  //取消
                    backFinish();
                    break;
                case R.id.img_set_signature_help:
                    show_signature_help_dialog(view);
                    break;
            }
        }
    };

    /**
     * 将书写签名保存到SD卡上，跳转到截取签名的界面，对签名进行截取并上传保存
     */
    private void save_signatuere() {
        ImageUtils.saveSignatrueImage(mContext, pain.getCachebBitmap(), 100);//保存图片到sdCard
        Intent intent = new Intent(mContext, SaveSignatureActivity.class);
        intent.putExtra("ecsignimage", ImageUtils.imageSignature);
        intent.putExtra(Constants.SIGN_TYPE, Constants.SIGN_EC);
        startActivityForResult(intent, ImageUtils.REQUEST_CODE_CROP_IMAGE);
        finish();
    }

    /**
     * 显示设置签名的用法帮助的弹框
     */
    public void show_signature_help_dialog(View view) {
        View v1 = LayoutInflater.from(mContext).inflate(R.layout.popup_siganture_help, null);// 创建view
        final PopupWindow popupWindow = new PopupWindow(v1, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, true);// 实例化popupwindow的方法
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        // 这个方法是为了当点击除了popupwindow的其他区域可以关闭popupwindow的方法
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        View view_top = v1.findViewById(R.id.view_popup_signature_top);
        view_top.setVisibility(View.GONE);
        View view_cancle = v1.findViewById(R.id.view_popup_signature_cancle);
        View view_confirm = v1.findViewById(R.id.view_popup_signature_confirm);
        view_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        view_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
    }
}
